package com.zh.api.controller.family;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.service.UserService;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.AllergyDO;
import com.zh.familyInfo.domain.FamilyFamilyUserDO;
import com.zh.familyInfo.service.AllergyService;
import com.zh.familyInfo.service.FamilyFamilyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@Api("老人疾病信息【老人过敏史】")
public class AllergyController extends BaseController {
	@Autowired
	private AllergyService allergyService;
	@Autowired
	private UserService userService;
    @Autowired
    private FamilyFamilyUserService familyFamilyUserService;

	@Log("老人过敏史所有信息")
	@ResponseBody
	@RequestMapping(value = "listFamilyAllergy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO list(@RequestBody Map<String, Object> params){
		if (CheckEmptyUtil.isEmptys(params)) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
//        查询列表数据
        Query query = new Query(params);
		List<AllergyDO> allergyList = allergyService.list(query);
		int total = allergyService.count(query);
		PageUtils pageUtils = new PageUtils(allergyList, total);
		return ResultVO.success(pageUtils);
	}
	@Log("更新老人过敏史信息")
	@ResponseBody
	@RequestMapping(value = "updateFamilyAllergy", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateFamilyAllergy(@RequestBody AllergyDO allergy){
		if (allergy==null) {
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		allergyService.update(allergy);
		return ResultVO.success();
	}

	@Log("当前老人过敏史所有信息")
	@ResponseBody
	@RequestMapping(value = "editFamilyAllergyById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public  ResultVO edit(@RequestBody AllergyDO allergy){
		if(allergy==null || CheckEmptyUtil.isEmptys(allergy.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		AllergyDO allergys = allergyService.get(allergy.getId());
		return ResultVO.success(allergys);
	}

	@Log("保存老人过敏史信息")
	@ResponseBody
	@RequestMapping(value = "saveFamilyAllergy",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO saveFamilyAllergy(@RequestBody AllergyDO allergy){
//		必须绑定老人
		if(allergy==null || CheckEmptyUtil.isEmptys(allergy.getUserId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		allergy.setId(UUID.randomUUID().toString());
		if(allergyService.save(allergy)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}


	@Log("删除老人过敏史信息")
	@ResponseBody
	@RequestMapping(value = "removeFamilyAllergyById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO removeFamilyAllergyById(@RequestBody AllergyDO allergy){
		if(allergy==null || CheckEmptyUtil.isEmptys(allergy.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(allergyService.remove(allergy.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	@Log("检索老人信息【创建老人过敏史中选择的老人】")
	@ResponseBody
	@RequestMapping(value = "listFamilyAllOld",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO listFamilyAllOld(){
		Map<String, Object> query = new HashMap<>();
		List<FamilyFamilyUserDO> list = familyFamilyUserService.list(query);
		return ResultVO.success(list);
	}

	@Log("查询每列当前老人id的姓名")
	@ResponseBody
	@RequestMapping(value = "getFamilyUserById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO getFamilyUserById(@RequestBody FamilyFamilyUserDO familyFamilyUserDO){
		if(familyFamilyUserDO==null || CheckEmptyUtil.isEmptys(familyFamilyUserDO.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		return ResultVO.success(userService.get(familyFamilyUserDO.getId()));
	}
	@RequestMapping(value = "batchRemoveFamilyAllergyByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除过敏信息")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		String[] strs1=ids.toArray(new String[ids.size()]);
		if(allergyService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

}
